package com.example.myapplication.ui.news.NewsPresenter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;

import com.example.myapplication.R;
import com.example.myapplication.api.ApiConstants;
import com.example.myapplication.app.App;
import com.example.myapplication.app.AppConstant;
import com.example.myapplication.base.RxPresenter;
import com.example.myapplication.disklrucache.DiskCacheManager;
import com.example.myapplication.model.bean.NewsChannelTable;
import com.example.myapplication.model.rx.RxSubscriber;
import com.example.myapplication.ui.wight.LoadingDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by meishiyiyun on 2017/6/7.
 */

public class NewChannelPresenter  extends RxPresenter<NewChannelContract.View> implements  NewChannelContract.Presenter {
  private   Context mContext;
    public NewChannelPresenter(Context context) {
        this.mContext=context;
    }

    private Observable<List<NewsChannelTable>> lodeMineNewsChannels(){
        return  Observable.create((ObservableOnSubscribe<List<NewsChannelTable>>) e -> {
            DiskCacheManager manager = new DiskCacheManager(App.getInstance(),AppConstant.CHANNEL_MINE);
            ArrayList<NewsChannelTable> newsChannelTableList =  manager.getSerializable(AppConstant.CHANNEL_MINE);
          //  ArrayList<NewsChannelTable> newsChannelTableList = (ArrayList<NewsChannelTable>) ACache.get(App.getInstance()).getAsObject(AppConstant.CHANNEL_MINE);
            if (newsChannelTableList ==null){
                newsChannelTableList= (ArrayList<NewsChannelTable>) loadNewsChannelsStatic();
                manager.put(AppConstant.CHANNEL_MINE,newsChannelTableList);
              //  ACache.get(App.getInstance()).put(AppConstant.CHANNEL_MINE,newsChannelTableList);
            }
            e.onNext(newsChannelTableList);
            e.onComplete();
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }


    private  List<NewsChannelTable> loadNewsChannelsStatic(){
        List<String> channelName = Arrays.asList(App.getInstance().getResources().getStringArray(R.array.news_channel_name_static));
        List<String> channelId = Arrays.asList(App.getInstance().getResources().getStringArray(R.array.news_channel_id_static));
        ArrayList<NewsChannelTable>newsChannelTables=new ArrayList<>();
        for (int i = 0; i < channelName.size(); i++) {
            NewsChannelTable entity = new NewsChannelTable(channelName.get(i), channelId.get(i), ApiConstants.getType(channelId.get(i)), i <= 5, i, true);
            newsChannelTables.add(entity);
        }
        return newsChannelTables;
    }




    private  Observable<List<NewsChannelTable>> lodeMoreNewsChannels() {
        return  Observable.create((ObservableOnSubscribe<List<NewsChannelTable>>) e -> {
            DiskCacheManager manager = new DiskCacheManager(App.getInstance(),AppConstant.CHANNEL_MORE);
            ArrayList<NewsChannelTable> newsChannelTableList =manager.getSerializable(AppConstant.CHANNEL_MORE);
                  //  ArrayList<NewsChannelTable> newsChannelTableList = (ArrayList<NewsChannelTable>) ACache.get(App.getInstance()).getAsObject(AppConstant.CHANNEL_MORE);
            if (newsChannelTableList ==null){
                List<String> channelName = Arrays.asList(App.getInstance().getResources().getStringArray(R.array.news_channel_name));
                List<String> channelId = Arrays.asList(App.getInstance().getResources().getStringArray(R.array.news_channel_id));
                newsChannelTableList = new ArrayList<>();
                for (int i = 0; i < channelName.size(); i++) {
                    NewsChannelTable entity = new NewsChannelTable(channelName.get(i), channelId.get(i)
                            , ApiConstants.getType(channelId.get(i)), i <= 5, i, false);
                    newsChannelTableList.add(entity);
                    manager.put(AppConstant.CHANNEL_MORE,newsChannelTableList);
                }
            }
            e.onNext(newsChannelTableList);
            e.onComplete();
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }


    @Override
    public void requestDataMineNewsChannels() {
        Dialog dialog= LoadingDialog.showDialogForLoading((Activity) mContext,"加载中...",false,false);
        lodeMineNewsChannels().subscribe(new RxSubscriber<List<NewsChannelTable>>(dialog) {
            @Override
            protected void _onNext(List<NewsChannelTable> newsChannelTables) {
                mView.returnMineNewsChannels(newsChannelTables);
                dialog.dismiss();
            }

            @Override
            protected void _onError(String message) {
                mView.showError(message);
            }
        });

        lodeMoreNewsChannels().subscribe(new RxSubscriber<List<NewsChannelTable>>(dialog) {
            @Override
            protected void _onNext(List<NewsChannelTable> newsChannelTables) {
                mView.returnMoreNewsChannels(newsChannelTables);
                dialog.dismiss();
            }

            @Override
            protected void _onError(String message) {
                mView.showError(message);
            }
        });
    }

    @Override
    public void requestDataMoreNewsChannels(final ArrayList<NewsChannelTable> newsChannelTableList,final ArrayList<NewsChannelTable> moreChannelTableList) {
        Dialog dialog= LoadingDialog.showDialogForLoading((Activity) mContext,"加载中...",false,false);
        Updata(newsChannelTableList,moreChannelTableList).subscribe(new RxSubscriber<String>(dialog) {
            @Override
            protected void _onNext(String s) {
                EventBus.getDefault().post("更新");
            }

            @Override
            protected void _onError(String message) {

            }
        });
    }

    @Override
    public void requestDataMoreNewsupata(ArrayList<NewsChannelTable> newsChannelTableList,ArrayList<NewsChannelTable> moreChannelTableList) {
        Dialog dialog= LoadingDialog.showDialogForLoading((Activity) mContext,"加载中...",false,false);
        updateDb(newsChannelTableList,moreChannelTableList).subscribe(new RxSubscriber<String>(dialog) {
            @Override
            protected void _onNext(String s) {
                EventBus.getDefault().post("更新");
            }

            @Override
            protected void _onError(String message) {

            }
        });
    }




    private  Observable<String> Updata(final ArrayList<NewsChannelTable> newsChannelTableList,final ArrayList<NewsChannelTable> moreChannelTableList){
        return Observable.create((ObservableOnSubscribe<String>) e -> {
            DiskCacheManager manager = new DiskCacheManager(App.getInstance(),AppConstant.CHANNEL_MINE);
            manager.put(AppConstant.CHANNEL_MINE,newsChannelTableList);
            DiskCacheManager managers = new DiskCacheManager(App.getInstance(),AppConstant.CHANNEL_MORE);
            managers.put(AppConstant.CHANNEL_MORE,moreChannelTableList);
            e.onNext("");
            e.onComplete();
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());

    }



    private Observable<String> updateDb(final ArrayList<NewsChannelTable> mineChannelTableList,final ArrayList<NewsChannelTable> moreChannelTableList) {
        return Observable.create((ObservableOnSubscribe<String>) e -> {
            DiskCacheManager manager = new DiskCacheManager(App.getInstance(),AppConstant.CHANNEL_MINE);
            manager.put(AppConstant.CHANNEL_MINE,mineChannelTableList);
            DiskCacheManager managers = new DiskCacheManager(App.getInstance(),AppConstant.CHANNEL_MORE);
            managers.put(AppConstant.CHANNEL_MORE,moreChannelTableList);
            e.onNext("");
            e.onComplete();
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    }
}
